﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.ScriptEngine.Custom;
using DWF.Studio.ScriptEngine.Proxy;
using DWF.Studio.Share.Log;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.ScriptEngine
{
    public abstract class EngineProcessLoaderBase : IScriptEngineLoader
    {
        protected ScriptEngineProxyContext EngineContext
        {
            get;
            private set;
        }

        public EngineProcessLoaderBase(ScriptEngineProxyContext engineContext)
        {
            EngineContext = engineContext;
        }

        protected void SetEngineArguments(string arguments)
        {
            ArgumentsHandler.Instance.SetArguments(arguments);
        }

        //Oliver add 2021-06-24
        protected virtual void StartProcess(string arguments, ETargetPlatform platform, Dictionary<string, string> projectProperties)
        {
            string fullname = string.Empty;
            if (platform == ETargetPlatform.x86)
            {
                fullname = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DWF.Studio.ScriptEngineShell_x86.exe");
            }
            else if (platform == ETargetPlatform.x64)
            {
                fullname = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DWF.Studio.ScriptEngineShell_x64.exe");
            }
            else
            {
                return;
            }

            EngineContext.StartEngineProcess(fullname, arguments, projectProperties);
        }
        protected virtual void StartProcess(string arguments, ETargetPlatform platform)
        {
            string fullname = string.Empty;
            if (platform == ETargetPlatform.x86)
            {
                fullname = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DWF.Studio.ScriptEngineShell_x86.exe");
            }
            else if (platform == ETargetPlatform.x64)
            {
                fullname = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DWF.Studio.ScriptEngineShell_x64.exe");
            }
            else
            {
                return;
            }

            EngineContext.StartEngineProcess(fullname, arguments);
        }

        public virtual void Unload()
        {
            EngineContext.UnloadEngineProcess(5000);
        }

        public abstract IScriptEngine Load(string engineId);
        public abstract IScriptEngine Load(string[] resolutionPaths, ETargetPlatform platform);
        public abstract IScriptEngine Load(string[] resolutionPaths, string[] assemblyNames, ETargetPlatform platform);
        public abstract IScriptEngine Load(string[] resolutionPaths, string[] assemblyNames, string param, ETargetPlatform platform);
        public abstract IScriptEngine Load(string[] resolutionPaths, string[] assemblyNames, Dictionary<string, string> projectProperties, string param, ETargetPlatform platform);

    }
}
